import merge from 'deepmerge';
import {validate, constraints_ActionItem, constraints_ActionNote} from '../untils/validate'

const defaultState = {
  actionReport: {
    actionItemReport: {
      status: '',
      dataCount: 0,
      pageLength: 10,
      pageIndex: 0,
      data: [],
      message: {},
      formData: {
        actionItemForm: {
          status: '',
          data: {
            actionItem: '',
            respPerson: '',
            dueDate: '',
            associatedSupplier: '',
            status: '',
          },
        },
      },
    },
    actionNoteReport: {
      status: '',
      message: {},
      data: [],
      formData: {
        actionNoteForm: {
          status: '',
          data: {
            note: '',
            updateBy: '',
            createDate: '',
          },
        },
      },
    },
  },
  salesDashboard: {
    monthlyReport: {
      status: '',
      data: [],
    },
    dashboard: {
      status: '',
      data: [],
    },
  },
}

const reducer = (state, actionData) => {

  let rv = (!state) ? defaultState : merge({}, state);

  switch (actionData.type) {
    /* Action Item Panel */
    case 'PANEL_ACTIONITEM_DISPLAY':{

      let thisForm = rv['actionReport']['actionItemReport']['formData']['actionItemForm'];
      thisForm['status'] = actionData.status;

      if (actionData.formData != undefined){
        thisForm['data'] = actionData.formData;
      }else{
        thisForm['data'] = defaultState['actionReport']['actionItemReport']['formData']['actionItemForm']['data'];
      } 

      return rv;
      break;
    }
    case 'PANEL_ACTIONNOTE_DISPLAY':{
      let thisForm = rv['actionReport']['actionNoteReport']['formData']['actionNoteForm'];
      thisForm['status'] = actionData.status;

      if (actionData.formData != undefined){
        thisForm['data'] = actionData.formData;
      }else{
        thisForm['data'] = defaultState['actionReport']['actionNoteReport']['formData']['actionNoteForm']['data'];
      } 
      return rv;    
      break;
    }
    case 'PANEL_ACTIONITEM_FORM_CHANGE':{
      let thisForm = rv['actionReport']['actionItemReport']['formData']['actionItemForm'];
      thisForm['data'] = merge(thisForm['data'], actionData.formData);
      return rv;
      break;
    }
    case 'PANEL_ACTIONNOTE_FORM_CHANGE':{
      let thisForm = rv['actionReport']['actionNoteReport']['formData']['actionNoteForm'];
      thisForm['data'] = merge(thisForm['data'], actionData.formData);
      return rv;
      break;
    }
    case 'PANEL_ACTIONITEM_REQUEST':{
      let thisReport = rv['actionReport']['actionItemReport'];
      thisReport['status'] = actionData.status;

      switch(actionData.status){
        case 'RequestStart': {
          break;
        }
        case 'RequestEnd':{
          if (actionData.result.success) {
            // Clear form, clear message, assign the data
            thisReport['message'] = [];

            if (actionData.result.data != undefined) thisReport['data'] = actionData.result.data;
            if (actionData.result.dataCount != undefined) thisReport['dataCount'] = actionData.result.dataCount;
            if (actionData.result.pageIndex != undefined) thisReport['pageIndex'] = actionData.result.pageIndex;
            if (actionData.result.pageLength != undefined) thisReport['pageLength'] = actionData.result.pageLength;
          }else{
            thisReport['message'] = actionData.result.message;
          }
          break;
        }
      }
      return rv;
      break;
    }
    case 'PANEL_ACTIONNOTE_REQUEST':{
      let thisReport = rv['actionReport']['actionNoteReport'];
      thisReport['status'] = actionData.status;

      switch(actionData.status){
        case 'RequestStart': {
          break;
        }
        case 'RequestEnd':{
          // if request succeed, then show the data, otherwise show error msg
          if (actionData.result.success) {

            thisReport['message'] = [];
            
            if (actionData.result.data != undefined) thisReport['data'] = actionData.result.data;
            if (actionData.result.dataCount != undefined) thisReport['dataCount'] = actionData.result.dataCount;
            if (actionData.result.pageIndex != undefined) thisReport['pageIndex'] = actionData.result.pageIndex;
            if (actionData.result.pageLength != undefined) thisReport['pageLength'] = actionData.result.pageLength;
          }else{
            thisReport['message'] = actionData.result.message;
          }
          break;
        }
      }
      return rv;
      break;
    }
    default:{
      return rv;
    }
 
  }
}

export default reducer
